package com.app.qbake.module.video;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.wind.base.adapter.BaseDelegateRecyclerAdapter;
import com.wind.base.mvp.view.BasePageFragment;
import com.wind.base.request.BaseRequest;
import com.wind.coder.annotations.Api;
import com.wind.coder.annotations.Heros;
import com.wind.coder.annotations.Param;
import com.wind.data.video.VideoListRequest;
import com.wind.data.video.VideoListResponse;
import com.app.qbake.R;
import com.app.qbake.module.square.activity.GoodsDetailActivity;
import com.app.qbake.module.video.adapter.VideoAdapter;
import com.app.qbake.module.video.di.VideoListComponent;
import com.app.qbake.module.video.event.VideoItemClickEvent;
import com.app.qbake.module.video.mvp.VideoListView;
import com.app.qbake.module.video.presenter.VideoListPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import javax.inject.Inject;

@Heros(
        param = @Param(
                viewCanonicalName = "com.app.qbake.module.video.mvp.VideoListView",
                responseCanonicalName = "com.wind.data.video.VideoListResponse",
                requestCanonicalName = "com.wind.data.video.VideoListRequest",
                page = true
        ),
        api = @Api(httpMethod = Api.HttpMethod.GET, url = "home/getVideo")
)
public class VideoListFragment extends BasePageFragment<VideoListView,VideoListPresenter,VideoListComponent,VideoListResponse>
implements VideoListView{
        public static final String ARGS_KEY_TYPE="args_key_type";
        private String mType;
        @Inject
        VideoListPresenter presenter;

        @Override
        public VideoListPresenter createPresenter() {
                return presenter;
        }

        @Override
        protected RecyclerView.LayoutManager getLayoutManager() {
                StaggeredGridLayoutManager manager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
                return manager;
        }

        @Override
        protected RecyclerView.ItemDecoration getItemDecoration() {
                return null;
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
                mType=getArguments().getString(ARGS_KEY_TYPE);
                super.onViewCreated(view, savedInstanceState);
                EventBus.getDefault().register(this);
        }

        @Override
        protected BaseDelegateRecyclerAdapter getAdapter() {
                VideoAdapter adapter=new VideoAdapter(getActivity(),mType);
                return adapter;
        }

        @Override
        protected BaseRequest buildRequest(boolean isFirstPage) {
                VideoListRequest request=new VideoListRequest();
                request.setType(mType);
                return request;
        }

        @Override
        protected int getLayoutRes() {
                return R.layout.fragment_video_list;
        }

        public static Fragment getInstance(String type){
                VideoListFragment fragment=new VideoListFragment();
                Bundle args=new Bundle();
                args.putString(ARGS_KEY_TYPE,type);
                fragment.setArguments(args);
                return fragment;
        }

        @Subscribe
        public void onVideoItemClick(VideoItemClickEvent videoItemClick){
                if (mType.equals(videoItemClick.getType()))
                        GoodsDetailActivity.start(getActivity(),videoItemClick.getGoods().getId());
        }
        @Override
        public void onDestroyView() {
                super.onDestroyView();
                EventBus.getDefault().unregister(this);
        }


}
